﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIB;
using System.Security.Cryptography;
using TobyEmden.Security.Encryption;

namespace LIB
{
    public class ConvertUtility
    {
        public static readonly string VALUE_JAN_TEXT_VN = "tháng 1";
        public static readonly string VALUE_FEB_TEXT_VN = "tháng 2";
        public static readonly string VALUE_MAR_TEXT_VN = "tháng 3";
        public static readonly string VALUE_APR_TEXT_VN = "tháng 4";
        public static readonly string VALUE_MAY_TEXT_VN = "tháng 5";
        public static readonly string VALUE_JUN_TEXT_VN = "tháng 6";
        public static readonly string VALUE_JUL_TEXT_VN = "tháng 7";
        public static readonly string VALUE_AUG_TEXT_VN = "tháng 8";
        public static readonly string VALUE_SEP_TEXT_VN = "tháng 9";
        public static readonly string VALUE_OCT_TEXT_VN = "tháng 10";
        public static readonly string VALUE_NOV_TEXT_VN = "tháng 11";
        public static readonly string VALUE_DEC_TEXT_VN = "tháng 12";
        //public static string[] ChuSo = new string[10] { " không", " một", " hai", " ba", " bốn", " năm", " sáu", " bẩy", " tám", " chín" };
        public static string[] ChuSo = new string[10] { " 0", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9" };
       
        public static string[] Tien = new string[6] { "", " nghìn", " triệu", " tỷ", " nghìn tỷ", " triệu tỷ" };
        
        public static string CreateRandomPassword(int passwordLength)
        {
            string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            char[] chars = new char[passwordLength];
            Random rd = new Random();

            for (int i = 0; i < passwordLength; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }

            return new string(chars);
        }

        public static string DESEncrypt(string plainText, string password)
        {
            string encrypted;
            TripleDESCryptoServiceProvider des;
            MD5CryptoServiceProvider hash;
            byte[] pwdhash, buff;

            hash = new MD5CryptoServiceProvider();
            pwdhash = hash.ComputeHash(UnicodeEncoding.Unicode.GetBytes(password));
            hash = null;

            des = new TripleDESCryptoServiceProvider();
            des.Key = pwdhash;
            des.Mode = CipherMode.ECB;

            buff = UnicodeEncoding.Unicode.GetBytes(plainText);
            encrypted = Convert.ToBase64String(
                des.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length)
                );

            des = null;
            return encrypted;
        }

        public static string DESDecrypt(string encrypted, string password)
        {
            string decrypted;
            TripleDESCryptoServiceProvider des;
            MD5CryptoServiceProvider hash;
            byte[] pwdhash, buff;

            hash = new MD5CryptoServiceProvider();
            pwdhash = hash.ComputeHash(UnicodeEncoding.Unicode.GetBytes(password));
            hash = null;

            des = new TripleDESCryptoServiceProvider();
            des.Key = pwdhash;
            des.Mode = CipherMode.ECB;

            buff = Convert.FromBase64String(encrypted);
            decrypted = UnicodeEncoding.Unicode.GetString(
                des.CreateDecryptor().TransformFinalBlock(buff, 0, buff.Length)
                );
            des = null;
            return decrypted;
        }
        public static string RC2Encrypt(string plainText)
        {
            string Aut = string.Empty;
            Crypto.Algorithm alg = new Crypto.Algorithm();
            alg = Crypto.Algorithm.RC2;
            Crypto.EncryptionAlgorithm = alg;
            Crypto.Encoding = Crypto.EncodingType.HEX;
            Crypto.Key = App_Constant.FUNNY;
            if (Crypto.EncryptString(plainText))
            {
                Aut = Crypto.Content;
            }
            return Aut;
        }

        public static string RC2Decrypt(string encrypted)
        {
            string Aut = string.Empty;
            Crypto.Algorithm alg = new Crypto.Algorithm();
            alg = Crypto.Algorithm.RC2;
            Crypto.EncryptionAlgorithm = alg;
            Crypto.Encoding = Crypto.EncodingType.HEX;
            Crypto.Key = App_Constant.FUNNY;
            Crypto.Content = encrypted.Trim();

            if (Crypto.DecryptString())
            {
                Aut = Crypto.Content;
            }
            return Aut;
        }
        public static string convertToVND(double money)
        {
            money = money * 1000;
            char[] charMoney = ToString(money).ToCharArray();
            int length = charMoney.Length;
            int du = length % 3;
            string result = "";
            for (int i = 0; i < length; i++)
            {
                if ((i + 1) % 3 == du && (i + 1) < length)
                    result += charMoney[i] + ".";
                else
                    result += charMoney[i];
            }
            return result;
        }
        public static double ToDouble(object obj, bool isForConvertVND)
        {
            double retVal;

            try
            {
                retVal = Math.Truncate(ConvertUtility.ToDouble(obj) * 1000) / 1000;
            }
            catch
            {
                retVal = 0;
            }

            return retVal;
        }
        public static string buidPaging2(int totalPage, int pageindex, string url)
        {
            string strPaging = "";

            if (totalPage <= 1)
            {
                return string.Empty;
            }

            if (pageindex <= 0)
            {
                pageindex = 1;
            }
            if (pageindex > totalPage)
            {
                pageindex = totalPage;
            }

            for (int i = 1; i <= totalPage; i++)
            {
                if (i == pageindex)
                {
                    strPaging += "<li class=\"current\"><a href='" + url + "&page=" + i + "' title=\"Trang " + i + "\" >" + i + "</a></li>";
                }
                else
                {
                    strPaging += "<li><a href='" + url + "&page=" + i + "' title=\"Trang " + i + "\">" + i + "</a></li>";
                }

                if (i == 2 && pageindex > 7)
                {
                    i = pageindex - 5;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }

                if ((i == pageindex + 4) && (pageindex < totalPage - 7))
                {
                    i = totalPage - 3;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }
            }


            string preDis = "<li><a href=\"javascript:void(0);\" class=\"none\">&lt; Prev</a></li>";
            string nextGo = "<li><a href='" + url + "&page=" + (pageindex + 1) + "' title=\"Trang " + (pageindex + 1) + "\" >Next &gt;</a></li>";
            string nextDis = "<li><a href=\"javascript:void(0);\" class=\"none\">Next &gt;</a></li>";
            string preGo = "<li><a href='" + url + "&page=" + (pageindex - 1) + "' title=\"Trang " + (pageindex - 1) + "\" >&lt; Prev</a></li>";

            if (pageindex == 1)
            {
                if (totalPage > 1)
                {
                    strPaging = preDis + strPaging + nextGo;
                }
                else
                {
                    strPaging = preDis + strPaging + nextDis;
                }
            }
            else if (pageindex == totalPage)
            {
                strPaging = preGo + strPaging + nextDis;
            }
            else
            {
                strPaging = preGo + strPaging + nextGo;
            }

            return "<ul class=\"paginator\">" + strPaging + "</ul>";

        }

        public static string buidPaging3(int totalPage, int pageindex, string url)
        {
            string strPaging = "";

            if (totalPage <= 1)
            {
                return string.Empty;
            }

            if (pageindex <= 0)
            {
                pageindex = 1;
            }
            if (pageindex > totalPage)
            {
                pageindex = totalPage;
            }

            for (int i = 1; i <= totalPage; i++)
            {
                if (i == pageindex)
                {
                    strPaging += "<li class=\"current\"><a href='" + url + "?page=" + i + "' title=\"Trang " + i + "\" >" + i + "</a></li>";
                }
                else
                {
                    strPaging += "<li><a href='" + url + "?page=" + i + "' title=\"Trang " + i + "\">" + i + "</a></li>";
                }

                if (i == 2 && pageindex > 7)
                {
                    i = pageindex - 5;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }

                if ((i == pageindex + 4) && (pageindex < totalPage - 7))
                {
                    i = totalPage - 3;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }
            }


            string preDis = "<li><a href=\"javascript:void(0);\" class=\"none\">&lt; Prev</a></li>";
            string nextGo = "<li><a href='" + url + "?page=" + (pageindex + 1) + "' title=\"Trang " + (pageindex + 1) + "\" >Next &gt;</a></li>";
            string nextDis = "<li><a href=\"javascript:void(0);\" class=\"none\">Next &gt;</a></li>";
            string preGo = "<li><a href='" + url + "?page=" + (pageindex - 1) + "' title=\"Trang " + (pageindex - 1) + "\" >&lt; Prev</a></li>";

            if (pageindex == 1)
            {
                if (totalPage > 1)
                {
                    strPaging = preDis + strPaging + nextGo;
                }
                else
                {
                    strPaging = preDis + strPaging + nextDis;
                }
            }
            else if (pageindex == totalPage)
            {
                strPaging = preGo + strPaging + nextDis;
            }
            else
            {
                strPaging = preGo + strPaging + nextGo;
            }

            return "<ul class=\"paginator\">" + strPaging + "</ul>";

        } 
        
        public static string buidPaging(int totalPage, int pageindex, string url)
        {
            string strPaging = "";

            if (totalPage <= 1)
            {
                return string.Empty;
            }

            if (pageindex <= 0)
            {
                pageindex = 1;
            }
            if (pageindex > totalPage)
            {
                pageindex = totalPage;
            }

            for (int i = 1; i <= totalPage; i++)
            {
                if (i == pageindex)
                {
                    strPaging += "<li class=\"current\"><a href='?page=" + i + "' title=\"Trang " + i + "\" >" + i + "</a></li>";
                }
                else
                {
                    strPaging += "<li><a href='?page=" + i + "' title=\"Trang " + i + "\">" + i + "</a></li>";
                }

                if (i == 2 && pageindex > 7)
                {
                    i = pageindex - 5;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }

                if ((i == pageindex + 4) && (pageindex < totalPage - 7))
                {
                    i = totalPage - 3;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }
            }


            string preDis = "<li><a href=\"javascript:void(0);\" class=\"none\">&lt; Prev</a></li>";
            string nextGo = "<li><a href='?page=" + (pageindex + 1) + "' title=\"Trang " + (pageindex + 1) + "\" >Next &gt;</a></li>";
            string nextDis = "<li><a href=\"javascript:void(0);\" class=\"none\">Next &gt;</a></li>";
            string preGo = "<li><a href='?page=" + (pageindex - 1) + "' title=\"Trang " + (pageindex - 1) + "\" >&lt; Prev</a></li>";

            if (pageindex == 1)
            {
                if (totalPage > 1)
                {
                    strPaging = preDis + strPaging + nextGo;
                }
                else
                {
                    strPaging = preDis + strPaging + nextDis;
                }
            }
            else if (pageindex == totalPage)
            {
                strPaging = preGo + strPaging + nextDis;
            }
            else
            {
                strPaging = preGo + strPaging + nextGo;
            }

            return "<ul class=\"paginator\">"+strPaging+"</ul>";

        }
        public static string buildSimplePaging(int totalPage, int pageindex)
        {
            string strPaging = "";

            if (totalPage <= 1)
            {
                return string.Empty;
            }

            if (pageindex <= 0)
            {
                pageindex = 1;
            }
            if (pageindex > totalPage)
            {
                pageindex = totalPage;
            }

            for (int i = 1; i <= totalPage; i++)
            {
                if (i == pageindex)
                {
                    strPaging += "<li class=\"current\"><a href='javascript:void(0)' onclick='doLoad(" + i + ")' title=\"Trang " + i + "\" >" + i + "</a></li>";
                }
                else
                {
                    strPaging += "<li><a href='javascript:void(0)' onclick='doLoad(" + i + ")' title=\"Trang " + i + "\">" + i + "</a></li>";
                }

                if (i == 2 && pageindex > 7)
                {
                    i = pageindex - 5;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }

                if ((i == pageindex + 4) && (pageindex < totalPage - 7))
                {
                    i = totalPage - 3;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }
            }


            string preDis = "<li><a href=\"javascript:void(0);\" class=\"none\">&lt; Prev</a></li>";
            string nextGo = "<li><a href='javascript:void(0)' onclick='doLoad(" + (pageindex + 1) + ")' title=\"Trang " + (pageindex + 1) + "\" >Next &gt;</a></li>";
            string nextDis = "<li><a href=\"javascript:void(0);\" class=\"none\">Next &gt;</a></li>";
            string preGo = "<li><a href='javascript:void(0)' onclick='doLoad(" + (pageindex - 1) + ")' title=\"Trang " + (pageindex - 1) + "\" >&lt; Prev</a></li>";

            if (pageindex == 1)
            {
                if (totalPage > 1)
                {
                    strPaging = preDis + strPaging + nextGo;
                }
                else
                {
                    strPaging = preDis + strPaging + nextDis;
                }
            }
            else if (pageindex == totalPage)
            {
                strPaging = preGo + strPaging + nextDis;
            }
            else
            {
                strPaging = preGo + strPaging + nextGo;
            }

            return "<ul class=\"paginator\">" + strPaging + "</ul>";

        }

        public static string buildSimplePaging(int loaiID,int totalPage, int pageindex)
        {
            string strPaging = "";

            if (totalPage <= 1)
            {
                return string.Empty;
            }

            if (pageindex <= 0)
            {
                pageindex = 1;
            }
            if (pageindex > totalPage)
            {
                pageindex = totalPage;
            }

            for (int i = 1; i <= totalPage; i++)
            {
                if (i == pageindex)
                {
                    strPaging += "<li class=\"current\"><a href='javascript:void(0)' onclick='doLoad(" +loaiID+","+ i + ")' title=\"Trang " + i + "\" >" + i + "</a></li>";
                }
                else
                {
                    strPaging += "<li><a href='javascript:void(0)' onclick='doLoad(" + loaiID + "," + i + ")' title=\"Trang " + i + "\">" + i + "</a></li>";
                }

                if (i == 2 && pageindex > 7)
                {
                    i = pageindex - 5;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }

                if ((i == pageindex + 4) && (pageindex < totalPage - 7))
                {
                    i = totalPage - 3;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }
            }


            string preDis = "<li><a href=\"javascript:void(0);\" class=\"none\">&lt; Prev</a></li>";
            string nextGo = "<li><a href='javascript:void(0)' onclick='doLoad(" + loaiID + "," + (pageindex + 1) + ")' title=\"Trang " + (pageindex + 1) + "\" >Next &gt;</a></li>";
            string nextDis = "<li><a href=\"javascript:void(0);\" class=\"none\">Next &gt;</a></li>";
            string preGo = "<li><a href='javascript:void(0)' onclick='doLoad(" + loaiID + "," + (pageindex - 1) + ")' title=\"Trang " + (pageindex - 1) + "\" >&lt; Prev</a></li>";

            if (pageindex == 1)
            {
                if (totalPage > 1)
                {
                    strPaging = preDis + strPaging + nextGo;
                }
                else
                {
                    strPaging = preDis + strPaging + nextDis;
                }
            }
            else if (pageindex == totalPage)
            {
                strPaging = preGo + strPaging + nextDis;
            }
            else
            {
                strPaging = preGo + strPaging + nextGo;
            }

            return "<ul class=\"paginator\">" + strPaging + "</ul>";

        }
        public static string buildNhatKyPaging(int totalPage,int pageIndex)
        {
            string temp="<div class='titleBBR' style='margin:5px 0px 20px 0px'><p class='dumBBR paging'><b>Trang</b>";
            for(int i=1;i<=totalPage;i++)
            {
                if(i==pageIndex)
                    temp+="<a href='?page="+i+"' class='active'>"+i+"</a>";
                else
                    temp+="<a href='?page="+i+"'>"+i+"</a>";
            }
            temp+="</div>";     
            return temp;
        }

        public static string buidFrontPaging(int totalPage, int pageindex, string url)
        {
            string strPaging = "";

            if (totalPage <= 1)
            {
                return string.Empty;
            }

            if (pageindex <= 0)
            {
                pageindex = 1;
            }
            if (pageindex > totalPage)
            {
                pageindex = totalPage;
            }

            for (int i = 1; i <= totalPage; i++)
            {
                if (i == pageindex)
                {
                    strPaging += "<li class=\"current\"><a href='" + i + ".aspx' title=\"Trang " + i + "\" >" + i + "</a></li>";
                }
                else
                {
                    strPaging += "<li><a href='" + i + ".aspx' title=\"Trang " + i + "\">" + i + "</a></li>";
                }

                if (i == 2 && pageindex > 7)
                {
                    i = pageindex - 5;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }

                if ((i == pageindex + 4) && (pageindex < totalPage - 7))
                {
                    i = totalPage - 3;
                    strPaging += "<li><a href=\"javascript:void(0);\" class=\"none\">...</a></li>";
                }
            }


            string preDis = "<li><a href=\"javascript:void(0);\" class=\"none\">&lt; Prev</a></li>";
            string nextGo = "<li><a href='" + (pageindex + 1) + ".aspx' title=\"Trang " + (pageindex + 1) + "\" >Next &gt;</a></li>";
            string nextDis = "<li><a href=\"javascript:void(0);\" class=\"none\">Next &gt;</a></li>";
            string preGo = "<li><a href='" + (pageindex - 1) + ".aspx' title=\"Trang " + (pageindex - 1) + "\" >&lt; Prev</a></li>";

            if (pageindex == 1)
            {
                if (totalPage > 1)
                {
                    strPaging = preDis + strPaging + nextGo;
                }
                else
                {
                    strPaging = preDis + strPaging + nextDis;
                }
            }
            else if (pageindex == totalPage)
            {
                strPaging = preGo + strPaging + nextDis;
            }
            else
            {
                strPaging = preGo + strPaging + nextGo;
            }

            return "<ul class=\"paginator\">" + strPaging + "</ul>";

        }

        public static string ExtractString(string source, int length)
        {
            string dest = String.Empty;
            if (length == 0 || source.Length == 0)
                return dest;
            if (source.Length < length)
                dest = source;
            else
            {
                string tmp = source.Substring(0, length);
                int nSub = tmp.Length - 1;
                while (tmp[nSub] != ' ')
                {
                    nSub--;
                    if (nSub == 0) break;
                }
                dest = tmp.Substring(0, nSub);
            }
            return dest;
        }

        public static string ConvertDateTimeVN(DateTime dtTime)
        {
            string ttime = dtTime.ToShortTimeString();
            int intDay = dtTime.Day;
            int intMonth = dtTime.Month;
            int intYear = dtTime.Year;
            string strReturnValue = "";

            switch (intMonth)
            {
                case 1: strReturnValue = VALUE_JAN_TEXT_VN; break;
                case 2: strReturnValue = VALUE_FEB_TEXT_VN; break;
                case 3: strReturnValue = VALUE_MAR_TEXT_VN; break;
                case 4: strReturnValue = VALUE_APR_TEXT_VN; break;
                case 5: strReturnValue = VALUE_MAY_TEXT_VN; break;
                case 6: strReturnValue = VALUE_JUN_TEXT_VN; break;
                case 7: strReturnValue = VALUE_JUL_TEXT_VN; break;
                case 8: strReturnValue = VALUE_AUG_TEXT_VN; break;
                case 9: strReturnValue = VALUE_SEP_TEXT_VN; break;
                case 10: strReturnValue = VALUE_OCT_TEXT_VN; break;
                case 11: strReturnValue = VALUE_NOV_TEXT_VN; break;
                case 12: strReturnValue = VALUE_DEC_TEXT_VN; break;
            }
            string strResult = ttime + " Ngày " + intDay.ToString() + " - " + strReturnValue + " - " + intYear.ToString();
            return strResult;
        }

        public static string ConvertDateTimeVNNotTime(DateTime dtTime)
        {
            int intDay = dtTime.Day;
            int intMonth = dtTime.Month;
            int intYear = dtTime.Year;
            string strReturnValue = "";

            switch (intMonth)
            {
                case 1: strReturnValue = VALUE_JAN_TEXT_VN; break;
                case 2: strReturnValue = VALUE_FEB_TEXT_VN; break;
                case 3: strReturnValue = VALUE_MAR_TEXT_VN; break;
                case 4: strReturnValue = VALUE_APR_TEXT_VN; break;
                case 5: strReturnValue = VALUE_MAY_TEXT_VN; break;
                case 6: strReturnValue = VALUE_JUN_TEXT_VN; break;
                case 7: strReturnValue = VALUE_JUL_TEXT_VN; break;
                case 8: strReturnValue = VALUE_AUG_TEXT_VN; break;
                case 9: strReturnValue = VALUE_SEP_TEXT_VN; break;
                case 10: strReturnValue = VALUE_OCT_TEXT_VN; break;
                case 11: strReturnValue = VALUE_NOV_TEXT_VN; break;
                case 12: strReturnValue = VALUE_DEC_TEXT_VN; break;
            }
            string strResult = "Ngày " + intDay.ToString() + " - " + strReturnValue + " - " + intYear.ToString();
            return strResult;
        }

        public static string ConvertTime(DateTime dtTime)
        {
            string ttime = dtTime.ToShortTimeString();
            return ttime;
        }

        public static string ConvertDateTimeVNs(DateTime dtTime)
        {
            int intDay = dtTime.Day;
            int intMonth = dtTime.Month;
            int intYear = dtTime.Year;
            string strResult = "Ngày " + intDay.ToString() + "." + intMonth.ToString() + "." + intYear.ToString();
            return strResult;
        }
        // calculate dtTime to now by Beo
        public static string CalculateTime(DateTime dtTime)
        {
            DateTime dtNow = DateTime.Now;
            string rt = string.Empty;
            if (DateTime.Compare(dtNow, dtTime) > 0)
            {
                TimeSpan ts = dtNow.Subtract(dtTime);
                if (ts.Days < 5)
                {
                    if (ts.Days > 0) rt = string.Format("Đăng {0} ngày {1} giờ trước", ts.Days, ts.Hours);
                    else if (ts.Hours > 0) rt = string.Format("Đăng {0} giờ {1} phút {2} giây trước", ts.Hours, ts.Minutes, ts.Seconds);
                    else if (ts.Minutes > 0) rt = string.Format("Đăng {1} phút {2} giây trước", ts.Hours, ts.Minutes, ts.Seconds);
                    else if (ts.Seconds > 0)
                    {
                        rt = string.Format("Đăng {2} giây trước", ts.Hours, ts.Minutes, ts.Seconds);
                    }
                    else
                    {
                        rt = "Vừa đăng";
                    }
                }
                else rt = ConvertDateTimeVN_All(dtTime);
            }
            else
            {
                rt = "Vừa đăng";
            }
            return rt;
        }

        public static string CalculateTimeEN(DateTime dtTime)
        {
            DateTime dtNow = DateTime.Now;
            string rt = string.Empty;
            if (DateTime.Compare(dtNow, dtTime) > 0)
            {
                TimeSpan ts = dtNow.Subtract(dtTime);
                if (ts.Days < 5)
                {
                    if (ts.Days > 0) rt = string.Format("Posted  {0} day {1} hours ago", ts.Days, ts.Hours);
                    else if (ts.Hours > 0) rt = string.Format("Posted  {0} hours {1} minutes  {2} seconds ago", ts.Hours, ts.Minutes, ts.Seconds);
                    else if (ts.Minutes > 0) rt = string.Format("Posted  {1} minutes {2} seconds ago", ts.Hours, ts.Minutes, ts.Seconds);
                    else if (ts.Seconds > 0)
                    {
                        rt = string.Format("Posted  {2} seconds ago", ts.Hours, ts.Minutes, ts.Seconds);
                    }
                    else
                    {
                        rt = "Immediately";
                    }
                }
                else rt = ConvertDateTimeEng_All(dtTime);
            }
            else
            {
                rt = "Immediately";
            }
            return rt;
        }

        public static string ConvertDateTimeVN_All(DateTime dtTime)
        {
            string ttime = dtTime.ToShortTimeString();
            int intDay = dtTime.Day;
            int intMonth = dtTime.Month;
            int intYear = dtTime.Year;
            string strResult = ttime + " Ngày " + intDay.ToString() + "." + intMonth.ToString() + "." + intYear.ToString();
            return strResult;
        }

        public static string ConvertDateTimeEng_All(DateTime dtTime)
        {
            string ttime = dtTime.ToShortTimeString();
            int intDay = dtTime.Day;
            int intMonth = dtTime.Month;
            int intYear = dtTime.Year;
            string strResult = ttime + "  " + intMonth.ToString() + "." + intDay.ToString() + "." + intYear.ToString();
            return strResult;
        }

        public static string ConvertDateTimeVN1(DateTime dtTime)
        {
            return "" + dtTime.Day + "/" + dtTime.Month + "/" + dtTime.Year;
        }
        
        public static string FormatTimeVn(DateTime dt, string defaultText)
        {
            if (ToDateTime(dt) != new DateTime(1900, 1, 1))
                return dt.ToString("dd-mm-yy");
            else
                return defaultText;
        }
        //public static string FormatTimeVn(DateTime datetime)
        //{
        //    string m = "";
        //    if (datetime.Minute > 9)
        //    {
        //        m = datetime.Minute.ToString();
        //    }
        //    else
        //    {
        //        m = "0" + datetime.Minute.ToString();
        //    }
        //    if (AppEnv.GetLanguage() == "vi-VN")
        //    {
        //        return " Cập nhật : " + datetime.Hour + ":" + m + " - " + datetime.ToString("dd/MM/yyyy");
        //    }
        //    else
        //    {
        //        return " Update at : " + datetime.Hour + ":" + m + " - " + datetime.ToString("dd/MM/yyyy");
        //    }

        //}

        public static string buildSimplePaging(int sumOfPage,int currentPage,string preStr,string nextStr)
        {
            string result = "";
            //users/6-1/yakana=>preStr=users/6-;nextStr-yakana
            //Page: <a href="#" class="select">1</a>-<a href="#">2</a>-<a href="#">3</a>-<a href="#">4</a>-<a href="#">5</a>-<a href="#">6</a>-<a href="#">7</a>-<a href="#">8</a>
            result += "Page: ";
            for (int i = 1; i <= sumOfPage; i++)
            {
                if(i==currentPage)
                    result+="<a href='"+preStr+i+"/"+nextStr+"' class='select'>"+i+"</a>-";
                else
                    result += "<a href='" + preStr + i + "/" + nextStr + "'>" + i + "</a>-";
            }
            return result.Substring(0,result.Length-1);
        }
        
       
        public static string ToStringWithoutFontBold(object obj)
        {
            string retVal;

            try
            {
                retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "").Replace("<span>", "").Replace("</span>", "");
            }
            catch
            {
                retVal = string.Empty;
            }

            return retVal;
        }
        public static string ToStringWithoutFontBold(object obj, int length)
        {
            string retVal;

            try
            {
                if (Convert.ToString(obj).Length > length)
                {
                    retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "").Substring(0, length).Replace("<span>", "").Replace("</span>", "");
                    retVal = retVal.Substring(0, retVal.LastIndexOf(" ")) + "..";
                }
                else
                {
                    retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "").Replace("<span>", "").Replace("</span>", "");
                }
            }
            catch
            {
                retVal = string.Empty;
            }

            return retVal;
        }
       
        public static int ToInt32(object obj)
        {
            int retVal = 0;

            try
            {
                retVal = Convert.ToInt32(obj);
            }
            catch
            {
                retVal = 0;
            }

            return retVal;
        }

        public static int ToInt32(object obj, int defaultValue)
        {
            int retVal = defaultValue;

            try
            {
                retVal = Convert.ToInt32(obj);
            }
            catch
            {
                retVal = defaultValue;
            }

            return retVal;
        }

        public static string ToString(object obj)
        {
            string retVal;

            try
            {
                retVal = Convert.ToString(obj);
            }
            catch
            {
                retVal = string.Empty;
            }

            return retVal;
        }

        public static DateTime ToDateTime(object obj)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj);
            }
            catch
            {
                retVal = DateTime.Now;
            }
            if (retVal == new DateTime(1, 1, 1)) return DateTime.Now;

            return retVal;
        }

        public static DateTime ToDateTime(object obj, DateTime defaultValue)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj);
            }
            catch
            {
                retVal = DateTime.Now;
            }
            if (retVal == new DateTime(1, 1, 1)) return defaultValue;

            return retVal;
        }

        public static bool ToBoolean(object obj)
        {
            bool retVal;

            try
            {
                retVal = Convert.ToBoolean(obj);
            }
            catch
            {
                retVal = false;
            }

            return retVal;
        }

        public static double ToDouble(object obj)
        {
            double retVal;

            try
            {
                retVal = Convert.ToDouble(obj);
            }
            catch
            {
                retVal = 0;
            }

            return retVal;
        }

        public static double ToDouble(object obj, double defaultValue)
        {
            double retVal;

            try
            {
                retVal = Convert.ToDouble(obj);
            }
            catch
            {
                retVal = defaultValue;
            }

            return retVal;
        }
        // Hàm đọc số thành chữ
        public static string DocTienBangChu(double SoTien, string strTail)
        {
            int lan, i;
            double so;
            string KetQua = "", tmp = "";
            int[] ViTri = new int[6];
            if (SoTien < 0) return "Số tiền âm !";
            if (SoTien == 0) return "Không đồng !";
            if (SoTien > 0)
            {
                so = SoTien;
            }
            else
            {
                so = -SoTien;
            }
            //Kiểm tra số quá lớn
            if (SoTien > 8999999999999999)
            {
                SoTien = 0;
                return "";
            }
            ViTri[5] = (int)(so / 1000000000000000);
            so = so - double.Parse(ViTri[5].ToString()) * 1000000000000000;
            ViTri[4] = (int)(so / 1000000000000);
            so = so - double.Parse(ViTri[4].ToString()) * +1000000000000;
            ViTri[3] = (int)(so / 1000000000);
            so = so - double.Parse(ViTri[3].ToString()) * 1000000000;
            ViTri[2] = (int)(so / 1000000);
            ViTri[1] = (int)((so % 1000000) / 1000);
            ViTri[0] = (int)(so % 1000);
            if (ViTri[5] > 0)
            {
                lan = 5;
            }
            else if (ViTri[4] > 0)
            {
                lan = 4;
            }
            else if (ViTri[3] > 0)
            {
                lan = 3;
            }
            else if (ViTri[2] > 0)
            {
                lan = 2;
            }
            else if (ViTri[1] > 0)
            {
                lan = 1;
            }
            else
            {
                lan = 0;
            }
            for (i = lan; i >= 0; i--)
            {
                tmp = DocSo3ChuSo(ViTri[i]);
                KetQua += tmp;
                if (ViTri[i] != 0) KetQua += Tien[i];
                if ((i > 0) && (!string.IsNullOrEmpty(tmp))) KetQua += " ";//&& (!string.IsNullOrEmpty(tmp))
            }
            if (KetQua.Substring(KetQua.Length - 1, 1) == " ") KetQua = KetQua.Substring(0, KetQua.Length - 1);
            KetQua = KetQua.Trim() + strTail;
            return KetQua.Substring(0, 1).ToUpper() + KetQua.Substring(1);
        }

        // Hàm đọc số có 3 chữ số
        private static string DocSo3ChuSo(int baso)
        {
            int tram, chuc, donvi;
            string KetQua = "";
            tram = (int)(baso / 100);
            chuc = (int)((baso % 100) / 10);
            donvi = baso % 10;
            if ((tram == 0) && (chuc == 0) && (donvi == 0)) return "";
            if (tram != 0)
            {
                KetQua += ChuSo[tram] + " trăm";
                if ((chuc == 0) && (donvi != 0)) KetQua += " linh";
            }
            if ((chuc != 0) && (chuc != 1))
            {
                KetQua += ChuSo[chuc] + " mươi";
                if ((chuc == 0) && (donvi != 0)) KetQua = KetQua + " linh";
            }
            if (chuc == 1) KetQua += " mười";
            switch (donvi)
            {
                case 1:
                    if ((chuc != 0) && (chuc != 1))
                    {
                        KetQua += " mốt";
                    }
                    else
                    {
                        KetQua += ChuSo[donvi];
                    }
                    break;
                case 5:
                    if (chuc == 0)
                    {
                        KetQua += ChuSo[donvi];
                    }
                    else
                    {
                        KetQua += " lăm";
                    }
                    break;
                default:
                    if (donvi != 0)
                    {
                        KetQua += ChuSo[donvi];
                    }
                    break;
            }
            return KetQua;
        }
    }
}
